﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using NaverOpenApiLibraryPortableSampleForWindowsPhone8.Resources;
using Microsoft.Phone.Tasks;
using System.Text.RegularExpressions;

using NaverOpenApiLibrary.Services;
using NaverOpenApiLibrary.Models;

namespace NaverOpenApiLibraryPortableSampleForWindowsPhone8
{
    public partial class MainPage : PhoneApplicationPage
    {
        // ****************************************************
        // REPLACE BELOW WITH YOUR API KEY
        // API Keys can be issued at: http://dev.naver.com/openapi/
        private const string SEARCH_API_KEY = "4f60b03829def6e96147d436882f10ec";
        // ****************************************************

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            KinService ks = new KinService(SEARCH_API_KEY);
            ks.ApiServiceCompleted += (s, kin) =>
                {
                    // kin이 검색 결과입니다. 나머지는 여러분의 몫!
                    #region Populating Search Result
                    
                    var kins = new List<KinItem>(kin.Channel.Items);
                    foreach (var item in kins)
                    {
                        item.Title = Regex.Replace(item.Title, "<.*?>", string.Empty);
                        item.Description = Regex.Replace(item.Description, "<.*?>", string.Empty);
                    }

                    var ignore = Dispatcher.BeginInvoke(() =>
                        {
                            SearchResultListBox.ItemsSource = kins;
                        });
                    #endregion
                };

            ks.GetKin("그린팩토리");
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var senderListBox = sender as ListBox;
            var item = senderListBox.SelectedItem as KinItem;
            
            WebBrowserTask wbt = new WebBrowserTask();
            wbt.Uri = new Uri(item.Link);
            wbt.Show();
        }

        




        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}